//
// FindGui.cs
//
// Author: 
//  Fredy Ramirez <framirez@pacificcoolwave.com>
//
// Copyright (C) 2011 Fredy Ramirez (http://www.pacificcoolwave.com)
//
//  Creation date: Sep 30, 2011
//  Last modification: 

/*
The MIT License

Copyright (C) 2011 Fredy Ramirez (http://www.pacificcoolwave.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
 */

using System;
using Gtk;
using Mono.Addins;

namespace Pcw.Addins.Search
{
	internal class FindGui : Window
	{
		private FindText find;
		private static bool shown = false;
		
		private Entry ent_find;
		private Entry ent_replace;
		private CheckButton chk_regex;
		private CheckButton chk_case;
		//private Button btn_replace;
		private Button btn_replace_all;
		private Label lbl_replace;
		
		internal static bool IsShown {
			get {
				return shown;
			}
		}
		
		internal FindGui (FindText findText, bool isReplace, Window parent) : base("")
		{
			find = findText;
			
			if (isReplace)
				Title = AddinManager.CurrentLocalizer.GetString ("Replace");
			else
				Title = AddinManager.CurrentLocalizer.GetString ("Find");
			BorderWidth = 10;
			Modal = false;
			TransientFor = parent;
			SkipPagerHint = true;
			SkipTaskbarHint = true;
			WindowPosition = WindowPosition.Center;
			Resizable = false;
			AllowGrow = false;
			DestroyWithParent = true;
			
			// TODO Set Opacity in config file
			// Opacity = 0.9; // Vulues 0.1 to 1.0
			
			
			DeleteEvent += delegate(object o, DeleteEventArgs args) {
				CloseWindow();
			};
			
			VBox vb = new VBox(false, 10);
			
			vb.PackStart(BuildLabelEntryBox(),false,false,0);
			vb.PackStart(BuildCheckBox("Regex", AddinManager.CurrentLocalizer.GetString ("Use Regex")),false,false,0);
			vb.PackStart(BuildCheckBox("Case", AddinManager.CurrentLocalizer.GetString ("Ignore case")),false,false,0);
			vb.PackStart(BuildButtonBox(),false,false,0);
			
			Add(vb);
			ShowAll();
			
			ShowControls(isReplace);
			
			shown = true;
		}
		
		internal void Close ()
		{
			CloseWindow();
		}
		
		private void ShowControls(bool isReplace)
		{
			lbl_replace.Visible = isReplace;
			ent_replace.Visible = isReplace;
			//btn_replace.Visible = isReplace;
			btn_replace_all.Visible = isReplace;
		}
		
		private Table BuildLabelEntryBox()
		{
			Table tb = new Table(2,2, false);
			tb.BorderWidth = 10;
			tb.ColumnSpacing = 10;
			tb.RowSpacing = 10;
			
			tb.Attach(BuildLabel("Find", AddinManager.CurrentLocalizer.GetString ("Search for:")), 0, 1, 0 , 1);
			tb.Attach(BuildEntry("Find"), 1, 2, 0, 1);
			tb.Attach(BuildLabel("Replace", AddinManager.CurrentLocalizer.GetString ("Replace with:")), 0, 1, 1 , 2);
			tb.Attach(BuildEntry("Replace"), 1, 2, 1, 2);
			
			return tb;
		}
		
		private HBox BuildButtonBox()
		{
			HBox hb = new HBox(false,10);
			
			hb.PackEnd(BuildButton("Find", AddinManager.CurrentLocalizer.GetString ("Find")),false,false,0);
			//hb.PackEnd(BuildButton("Replace", "Replace"),false,false,0);
			hb.PackEnd(BuildButton("ReplaceAll", AddinManager.CurrentLocalizer.GetString ("Replace All")),false,false,0);
			hb.PackEnd(BuildButton("Close", AddinManager.CurrentLocalizer.GetString ("Close")),false,false,0);
			
			return hb;
		}
		
		private Button BuildButton(string name, string label)
		{
			Button btn = new Button(label);
			btn.Name = name;
			btn.Clicked += btn_clicked;
			
			switch(name)
			{
				case "Replace":
					//btn_replace = btn;
					break;
				case "ReplaceAll":
					btn_replace_all = btn;
					break;
			}
			
			return btn;
		}
		
		
		private HBox BuildLabel (string name, string text)
		{
			Label lbl = new Label(text);
			lbl.Name = name;
			if (name == "Replace")
				lbl_replace = lbl;
					
			return BuildHBox (lbl);
			
		}
		
		private Entry BuildEntry(string name)
		{
			Entry ent = new Entry();
			ent.Name = name;
			
			switch(name)
			{
				case "Find":
					ent_find = ent;
					break;
				case "Replace":
					ent_replace = ent;
					break;
			}
			
			return ent;
		}		

		private HBox BuildCheckBox(string name, string text)
		{
			CheckButton chk = new CheckButton(text);
			switch(name)
			{
				case "Regex":
					chk_regex = chk;
					break;
				case "Case":
					chk_case = chk;
					break;
			}			
			return BuildHBox(chk);
		}
		
		private HBox BuildHBox(Widget wg)
		{
			HBox h = new HBox(false,10);
			h.PackStart(wg,false,false,0);
			return h;
		}
		
		private void btn_clicked( object obj, EventArgs args)
		{
			Button btn = (Button) obj;
			
			switch(btn.Name) 
			{
				case "Find":
					find.Find(ent_find.Text, chk_case.Active, chk_regex.Active);
					break;
				
				case "ReplaceAll":
					find.Replace (ent_find.Text, ent_replace.Text, chk_case.Active, chk_regex.Active);
					break;	
				
				case "Close":
					CloseWindow();
					break;
			}
		}
		
		private void CloseWindow()
		{
			shown = false;
			Destroy();
		}
	}	
}

